import Dialog from "../../../component/vant/dialog/dialog";

let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cargoId: null,
        cargoNo: "",
        remark: "",
        purchasePrice: "",
        wholesalePrice: "",
        status: "",
        colors: "",
        sizes: "",
        picUrl: '',
        fileList: [],
        btnDisable: false,

        //action-sheet
        actions: [
            {name: '新增', type: "add"},
            {name: '工作台', type: "homepage"},
        ],
        actionSheetShow: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let cargoId = options.cargoId
        if (cargoId && cargoId != 'undefined' && cargoId != null) {
            this.data.cargoId = cargoId
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.queryData()
    },
    queryData: function () {
        let _this = this
        app.formGet('/app/cargo/getByCargoId?cargoId=' + this.data.cargoId, null).then(res => {
            if (res.code === 0) {
                console.log("res.data: %o", res.data)
                if (res.data != null) {
                    let imageObj = {}
                    let tempFileList = []
                    let picUrl = res.data.picUrl
                    imageObj.url = picUrl
                    imageObj.isImage = true
                    tempFileList.push(imageObj)
                    _this.setData({
                        fileList: tempFileList,
                        picUrl: picUrl,
                        id: res.data.id,
                        cargoNo: res.data.cargoNo,
                        cargoName: res.data.cargoName,
                        remark: res.data.remark,
                        status: res.data.status,
                        purchasePrice: res.data.purchasePrice,
                        wholesalePrice: res.data.wholesalePrice,
                        colors: res.data.colorList,
                        sizes: res.data.sizeList,
                    })
                }
            } else {
                console.log("error")
                app.message(res.message, 'error')
            }
        })
    },
    picAfterRead(event) {
        const {
            file
        } = event.detail;
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        let _this = this
        wx.uploadFile({
            url: app.globalData.baseAPI + '/app/upload/pic', // 仅为示例，非真实的接口地址
            filePath: file.url,
            name: 'file',
            formData: {},
            header: {
                'Authorization': wx.getStorageSync('token')
            },
            success(res) {
                // 上传完成需要更新 fileList
                let result = JSON.parse(res.data)
                if (result.code === 0) {
                    let imageObj = {}
                    let tempFileList = []
                    imageObj.url = result.data
                    imageObj.isImage = true
                    tempFileList.push(imageObj)
                    _this.setData({
                        fileList: tempFileList,
                        picUrl: result.data
                    })
                }
            },
        });
    },
    deleteFile: function (e) {
        let index = e.detail.index
        let fileList = this.data.fileList
        fileList.splice(index, 1)
        this.setData({
            fileList: fileList
        })
    },
    moreFun: function () {
        this.setData({
            actionSheetShow: true
        })
    },
    actionSelect: function (e) {
        let type = e.detail.type
        if (type == 'add') {
            wx.redirectTo({
                url: "/pages/cargo/edit/cargoEdit"
            })
        } else if (type == 'homepage') {
            wx.switchTab({
                url: "/pages/index/index"
            })
        }
    },
    actionCancel() {
        this.setData({actionSheetShow: false});
    },
    actionClose: function () {
        this.setData({actionSheetShow: false});
    },
    cargoDown: function () {
        let _this = this
        let data = {}
        data.id = this.data.cargoId
        Dialog.confirm({
            title: '确认要下架?',
            message: '下架后，入库单、销售单中无法选择此商品',
        })
            .then(() => {
                app.formPost('/app/cargo/off', data).then(res => {
                    if (res.code === 0) {
                        _this.onShow()
                        app.message(res.message, 'success')
                    } else {
                        app.message(res.message, 'error')
                    }
                }).catch(e => {
                    _this.setData({
                        spinShow: false
                    });
                    app.message(e, 'error')
                })
            })
            .catch(() => {
                // on cancel
            });
        return false;
    },
    cargoUp: function () {
        let _this = this
        let data = {}
        data.id = this.data.cargoId
        app.formPost('/app/cargo/on', data).then(res => {
            if (res.code === 0) {
                _this.onShow()
                app.message(res.message, 'success')
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
        return true;
    },
    handleCargoEdit: function () {
        wx.navigateTo({
            url: "/pages/cargo/edit/cargoEdit?cargoId=" + this.data.cargoId
        })
    }
})